package com.hdu.xxlJobLocalCacheFix.holder;

import com.google.common.base.Preconditions;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class ApplicationContextHolder implements ApplicationContextAware {

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    public static <T> T getBean(String name, Class<T> clazz) {
        return getInstance().getBean(name, clazz);
    }

    public static ApplicationContext getInstance() {
        Preconditions.checkNotNull(context, "ApplicationContext is null, please check if the spring container is started.");
        return context;
    }
}
